package com.tju.resource.oauth.controller.tb;

import com.tju.resource.annotation.Log;
import com.tju.resource.model.TbQcOrder;
import com.tju.resource.business.tb_qc_order.TbQcOrderService;
import com.tju.resource.business.tb_report_order.TbReportOrderService;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.tool.enum_utils.FunctionType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

@RestController
public class TbQcOrderController {

    public static final String prefix = "qc";

    @Autowired
    TbQcOrderService service;

    @Autowired
    TbReportOrderService repService;
    @Autowired
    private TbReportOrderService tbReportOrderService;
    @PostMapping(prefix + "/getUnApp")
    @PreAuthorize("hasAuthority('sys:quality:qc:list')")
    @Log(type = FunctionType.List)
    public Result<Object> getUnApp(@RequestBody Map<String, String> params) {
        return tbReportOrderService.selectTbReportOrder(params);
    }

    @PostMapping(prefix + "/getAppLog")
    @PreAuthorize("hasAuthority('sys:quality:qc:list')")
    @Log(type = FunctionType.List)
    public Result<Object> getAppLog(@RequestBody Map<String, String> params) {
        return service.getAppLog(params);
    }

    @PostMapping(prefix + "/add")
    @PreAuthorize("hasAuthority('sys:quality:qc:add')")
    @Log(type = FunctionType.Add)
    public Result<Object> add(@RequestBody TbQcOrder qco) {
        qco.setCreateTime(new Date());

        if (!StringUtils.isBlank(qco.getFinished())) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "新增转序单不可设置状态!");
        }

        return service.add(qco);
    }

    @PostMapping(prefix + "/upd")
    @PreAuthorize("hasAuthority('sys:quality:qc:edit')")
    @Log(type = FunctionType.Add)
    public Result<Object> upd(@RequestBody TbQcOrder qco) {
        qco.setUpdateTime(null);

        return service.upd(qco);
    }

    @PostMapping(prefix + "/app")
    @PreAuthorize("hasAuthority('sys:quality:qc:approve')")
    @Log(type = FunctionType.FirstInstance)
    public Result<Object> app(@RequestBody TbQcOrder qco) {
        return service.app(qco);
    }

    @PostMapping(prefix + "/del")
    @PreAuthorize("hasAuthority('sys:quality:qc:delete')")
    @Log(type = FunctionType.Delete)
    public Result<Object> del(@RequestBody Map<String, String> params) {
        TbQcOrder qco = new TbQcOrder();

        qco.setId(Long.parseLong(params.get("id")));

        return service.del(qco);
    }

}
